//
//  BIZOfficialAnswerCell.m
//  biz
//
//  Created by zhangfuwei on 2017/7/11.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZOfficialAnswerCell.h"

@interface BIZOfficialAnswerCell ()<UIWebViewDelegate>
/** professor */
@property (nonatomic, strong) UIButton *professorButton;
/** bgView */
@property (nonatomic, strong) UIView *webBgView;

/** answerContent */
@property (nonatomic, strong) UIWebView *webview;
/** appendQuestion */
@property (nonatomic, strong) UIButton *appendQuestionButton;
/** time */
@property (nonatomic, strong) UIButton *timeButton;
/** block */
@property (nonatomic, copy) void(^block)() ;
/** index */
@property (nonatomic, strong) NSIndexPath *indexPath;

@end

@implementation BIZOfficialAnswerCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initSubViews];

    }
    return self;
}


- (void)initSubViews {

    [self.contentView addSubview:self.professorButton];
    [self.contentView addSubview:self.webBgView];
    [self.contentView addSubview:self.timeButton];
    [self.contentView insertSubview:self.appendQuestionButton aboveSubview:self.timeButton];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    [self.webBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.professorButton.mas_bottom).priorityHigh();
        make.left.right.equalTo(self.contentView);
        make.height.equalTo(@(0)).priorityHigh();
    }];
    
    [self.timeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.webBgView.mas_bottom).offset(26).priorityHigh();
        make.height.equalTo(@35).priorityHigh();
        make.left.right.bottom.equalTo(self.contentView);
    }];
    
    [self.appendQuestionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-15);
        make.height.equalTo(@50);
        make.centerY.equalTo(self.timeButton).offset(-18);
    }];

}


#pragma mark -- set方法

- (void)setAnswerModel:(BIZConsultRecordModel *)answerModel {
    _answerModel = answerModel;
    if (answerModel == nil) {
        return;
    }
    
    NSString *htmlStr ;
    switch (_indexPath.row) {
        case 1: // 已回答
            htmlStr = answerModel.answer;
            break;
        case 3: // 第一次追问回答
            htmlStr = answerModel.firstAns;
            break;
        case 5:
            htmlStr = answerModel.secondAns;
            break;
        case 7:
            htmlStr = answerModel.thirdAns;
            break;
        default:
            break;
    }

    
    if (!self.webview.isLoading && htmlStr != nil && ![htmlStr isEqualToString:@""]) {
        NSString *HTML = [htmlStr stringByReplacingOccurrencesOfString:@"\n" withString:@"<br>"];
        DeLog(@"html--%@",HTML);
        [self.webview loadHTMLString:HTML baseURL:nil];
    }
    
    /** stage	int	必须	答案阶段：当前阶段 ：0 初始  1 已回答 2 用户第一次追问 3咨询师回答了第一次追问 4 用户第二次追问 5 咨询师回答了第二次追问 6 用户第三次追问 7 咨询师回答了第三次追问
     */
    BOOL hidden ;
    switch (answerModel.stage.intValue) {
        
        case 5: // 回答了第二次追问
            hidden = _indexPath.row == 5 ? NO:YES;
            break;
        case 3: // 回答了第一次追问
            hidden = _indexPath.row == 3 ? NO:YES;
            break;
        case 1: // 已回答
            hidden = NO;
            break;
        default:
            hidden = YES;
            break;
    }
    // MARK:y隐藏时间按钮&& 追问按钮
    
    _appendQuestionButton.hidden = hidden;
    [_timeButton setTitle:[NSDate stringFromTimestamp:answerModel.ansDate] forState:UIControlStateNormal];

    if (_indexPath.row >1) {
        _timeButton.hidden = YES;

        if (hidden) {
            
            [_timeButton mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@0);
            }];
        }
    }else {
        
        _timeButton.hidden = NO;
        [_timeButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@35);
        }];
    }
    [self layoutIfNeeded];
 }

- (void)setCacheHeight:(CGFloat)cacheHeight {
    _cacheHeight = cacheHeight;
    if (!cacheHeight) return;
    DeLog(@"缓存高度---%lf",cacheHeight);
    [_webBgView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(cacheHeight));
    }];
    [self layoutIfNeeded];
}
#pragma mark - class method
+ (BIZOfficialAnswerCell *)cellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath appendQusetion:(void (^)())appendBlock
{
    BIZOfficialAnswerCell *cell = [tableView dequeueReusableCellWithIdentifier:kOfficialAnswerCellID forIndexPath:indexPath];
    cell.block = appendBlock;
    cell.indexPath = indexPath;
    return cell;
}

#pragma mark -- uiwebView 代理


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if(!webView.isLoading){
    
        CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
        
        if (_cacheHeight)  return;
        
        DeLog(@"最终的webview高度%ld--%lf",_indexPath.row,height);
        if ( height != _webBgView.height) {

            [_webBgView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@(height));
            }];

            [[NSNotificationCenter defaultCenter] postNotificationName:kOfficialConsultDetailNotification object:nil userInfo:@{@"indexPath":_indexPath,@"newH":@(height)}];
        }
    }
}

#pragma mark -- Lazyload

- (UIButton *)professorButton {
    if (!_professorButton) {
        _professorButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 30)];
        _professorButton.backgroundColor = [UIColor zfw_ColorWithHex:0xf9f9f9 alpha:1.0];
        _professorButton.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
        _professorButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _professorButton.titleLabel.font = CHINESE_SYSTEM(13);
        [_professorButton setTitleColor:[UIColor zfw_ColorWithHex:0x666666 alpha:1.0] forState:UIControlStateNormal];
        [_professorButton setTitle:@"专家回答" forState:UIControlStateNormal];

    }
    return _professorButton;
}

- (UIWebView *)webview {
    if (!_webview) {
        _webview = [[UIWebView alloc] init];
        _webview.backgroundColor = [UIColor whiteColor];
        _webview.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
        _webview.multipleTouchEnabled = NO;
        _webview.autoresizesSubviews = YES;
        _webview.opaque = NO;
        _webview.scrollView.scrollEnabled = NO;
        _webview.scrollView.showsHorizontalScrollIndicator = NO;
        _webview.scrollView.showsVerticalScrollIndicator = NO;
        _webview.scrollView.bounces = NO;
        _webview.scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        _webview.delegate = self;
    }
    return _webview;
}

- (UIButton *)appendQuestionButton {
    if (!_appendQuestionButton) {
        _appendQuestionButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_appendQuestionButton addTarget:self action:@selector(appendQuestion) forControlEvents:UIControlEventTouchUpInside];
        [_appendQuestionButton setImage:[UIImage imageNamed:@"official_rapidConsult_appendQuestion"] forState:UIControlStateNormal];
    }
    return _appendQuestionButton;
}

- (void)appendQuestion {
    if (_block) {
        _block();
    }
}

- (UIButton *)timeButton {
    if (!_timeButton) {
        _timeButton = [[UIButton alloc] init];
        
        _timeButton.layer.borderColor = [UIColor zfw_ColorWithHex:0xeeedee alpha:1.0].CGColor;
        _timeButton.layer.borderWidth = 1.0f;
        _timeButton.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
        _timeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        
        _timeButton.titleLabel.font = CHINESE_SYSTEM(10);
        [_timeButton setTitleColor:[UIColor zfw_ColorWithHex:0x999999 alpha:1.0] forState:UIControlStateNormal];
    }
    return _timeButton;
}

- (UIView *)webBgView {
    if (!_webBgView) {
        _webBgView = [[UIView alloc] init];
        [_webBgView addSubview:self.webview];
        [_webview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(_webBgView);
        }];
    }
    return _webBgView;
}
@end
